/*! ******************************************************************************
 *
 * Created on 2022-05-28 10:50
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.pentaho.di.ui.spoon.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.pentaho.di.core.EngineMetaInterface;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.ui.core.gui.GUIResource;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.di.ui.util.DialogPositionUtil;
import org.yzbdl.lanius.ui.plugin.dialog.PluginBatchInstallDialog;

/**
 * 关闭资源库窗体
 *
 * @author yzbdl@yzbdl.ac.cn
 * @version 1.0
 * @date 2020-05-28 10:50
 */
public class CloseRepDialog extends Dialog {

    private static Class<?> PKG = Spoon.class;

    private Display display;

    private Shell shell, parent;

    private Composite compositeBtns;

    private Image imageCaptcha;

    private Button btnOk, btnDisconn, btnCancle;

    private Listener lsOk, lsDisconn, lsCancel;

    private int style;

    private static final int width = 600;

    private static final int height = 180;

    public CloseRepDialog(Shell parent) {
        super(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL | SWT.ON_TOP);
        this.parent = parent;
        this.style = SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL | SWT.ON_TOP;
    }

    public int open() {
        final int[] result = new int[1];
        display = parent.getDisplay();

        shell = new Shell(parent, style);
        shell.setImage(GUIResource.getInstance().getImageShellTitle());
        shell.setText(BaseMessages.getString(PKG, "Spoon.Dialog.WarnToCloseAllOption.Disconnect.Title"));

        DialogPositionUtil.getXyPoint(parent, width, height, shell);

        // 设置窗体的布局方式为GridLayout (GridLayout类似于网页中的table), 2列且列宽不等.
        GridLayout layout = new GridLayout(3, false);
        layout.marginTop = 5;
        layout.marginBottom = 5;
        layout.marginLeft = 10;
        layout.marginRight = 10;
        layout.horizontalSpacing = 10;
        layout.verticalSpacing = 5;
        shell.setLayout(layout);

        Label lbl = new Label(shell, 16777280);
        StringBuffer str = new StringBuffer("存在未保存的转换/作业\n");
        EngineMetaInterface activeMeta = Spoon.getInstance().getActiveMeta();
        if (activeMeta instanceof TransMeta) {
            TransMeta meta = (TransMeta)activeMeta;
            if (meta.hasMissingPlugins()) {
                str.append("当前转换/作业存在缺失插件,断开资源库后,将无法进行保存操作\n");
            }
        } else if (activeMeta instanceof JobMeta) {
            JobMeta jobMeta = (JobMeta)activeMeta;
            if (jobMeta.hasMissingPlugins()) {
                str.append("当前转换/作业存在缺失插件，断开资源库后,将无法进行保存或者下载的操作\n");
            }
        }
        str.append("请确认是否断开");
        lbl.setText(str.toString());
        GridData gd = new GridData(16777216, 16777216, true, true, 1, 1);
        lbl.setLayoutData(gd);

        // 设置所有按钮的容器
        compositeBtns = new Composite(shell, SWT.NONE);
        compositeBtns.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1));
        GridLayout flBtns = new GridLayout(3, true);
        flBtns.horizontalSpacing = 20;
        compositeBtns.setLayout(flBtns);

        // 设置确认保存并断开Button
        btnOk = new Button(compositeBtns, SWT.PUSH);
        btnOk.setText("保存所有并断开");
        btnOk.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

        lsOk = new Listener() {

            @Override
            public void handleEvent(Event event) {
                result[0] = 64;
                shell.dispose();
            }
        };
        btnOk.addListener(SWT.Selection, lsOk);

        // 设置直接断开Button
        btnDisconn = new Button(compositeBtns, SWT.PUSH);
        btnDisconn.setText("直接断开");
        btnDisconn.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        lsDisconn = new Listener() {

            @Override
            public void handleEvent(Event event) {
                result[0] = 128;

                shell.dispose();
            }
        };
        btnDisconn.addListener(SWT.Selection, lsDisconn);

        // 设置取消Button
        btnCancle = new Button(compositeBtns, SWT.PUSH);
        btnCancle.setText("取消");
        btnCancle.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        lsCancel = new Listener() {

            @Override
            public void handleEvent(Event event) {
                result[0] = 256;
                shell.dispose();
            }
        };
        btnCancle.addListener(SWT.Selection, lsCancel);

        // ==
        shell.layout();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        return result[0];
    }

    public void dispose() {
        shell.dispose();
    }

    public boolean isDisposed() {
        return this.shell.isDisposed();
    }
}
